<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active: active }">
      <span class="placeholder">{{
        `${changeResult.resultAddress || '请选择配送地址'}`
      }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        class="ellipsis"
        v-for="item in cityList"
        :key="item.code"
        @click="selectCity(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>
<script>
import { ref, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  // 将上下文中的emit结构出来
  setup (props, { emit }) {
    const cityList = ref([])
    const active = ref(false)
    let cacheListJSON = ''
    // 3. 交互选择
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '', // 地区名
      resultAddress: '' // 最终结果地址
    })
    const open = () => {
      active.value = true
    }
    const close = () => {
      active.value = false
    }
    function toggleDialog () {
      // if (!active.value) {
      //   open()
      // } else {
      //   close()
      // }
      active.value ? close() : open()
    }
    const getCityList = async () => {
      const res = await axios.get('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
      cityList.value = res.data
      // 深拷贝，引用类型的数据
      cacheListJSON = JSON.stringify(res.data)
    }
    getCityList()
    // city 数据
    // 1. level 标记了层级 0 省；1 市； 2 区；
    // 2. code   是唯一的标识
    // 3. name  渲染的名字
    // 4. areaList 存的是下一级想展示的名单

    // 省 =》 市 如何变换  将cityList覆盖成选择省份的areaList
    // 市 =》 区 如何变换  将cityList覆盖成选择市份的areaList
    // 区   level 2 点中需要的 关闭弹层

    const selectCity = (item) => {
      if (item.level === 0) {
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      }
      if (item.level === 1) {
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      }
      if (item.level === 2) {
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        changeResult.resultAddress = changeResult.provinceName + '-' + changeResult.cityName + '-' + changeResult.countyName
        // 确定选择之后把我们的数据传出去
        // 外面使用city组件之后 需要使用自定义函数@selAdddress获取子组件传出的值
        emit('selAddress', changeResult)
        close()
      }
      cityList.value = item.areaList
      // cityList.value = cityList.value.filter(item => item.name === item.name)[0].areaList
    }
    // 监听 active 一旦变为true 就重置数据
    watch(active, (newVal) => {
      if (newVal) {
        cityList.value = JSON.parse(cacheListJSON)
      }
    })

    return {
      active,
      cityList,
      toggleDialog,
      selectCity,
      changeResult
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
